package com.ccsoft;

import com.ccsoft.core.Downloader;
import com.ccsoft.core.DownloaderInfoThread;
import com.ccsoft.utils.LogUtils;

import java.util.Scanner;

// 拿腾讯 QQ 做测试下载：https://down.qq.com/qqweb/PCQQ/PCQQ_EXE/QQ9.5.3.28008.exe
public class Main {
    public static void main(String[] args) {
        String url = null;
        if( args == null || args.length == 0 ){
            for(;;){ // 用户没有输入有效字符则死循环
                LogUtils.info("输入下载地址后回车：");
                LogUtils.info("输入1读取 DownloaderInfo 中的 downloadSize");
                Scanner scanner = new Scanner(System.in);
                url = scanner.next();

                switch (url){ // 执行命令
                    case "1":
                        printDownloadSize();
                        break;
                    default:
                        LogUtils.info("输入了无效的命令！");
                }

                url = "";
                if( url != null && url.length() >0 ){
                    break;
                }
            }
        }else{
            url = args[0];
        }

        // 下载文件
        download(url);
    }

    private static void download(String url){
        Downloader downloader = new Downloader();
        String durationString = downloader.download(url);
        LogUtils.info("下载完毕，" + durationString);
    }

    private static void printDownloadSize(){
        LogUtils.info("当前 downloadSize = {}", DownloaderInfoThread.downloadSize);
    }
}
